package controllers.customer;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CommentService;
import controllers.AbstractController;
import domain.Comment;

@Controller
@RequestMapping("/comment/customer")
public class CommentCustomerController extends AbstractController {
	@Autowired
	private CommentService commentService;

	public CommentCustomerController() {
		super();
	}

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int availableItemId) {
		ModelAndView result;
		Comment comment;

		comment = commentService.create(availableItemId);
		result = new ModelAndView("comment/create");
		result.addObject("requestURI", "comment/customer/create.do");
		result.addObject("comment", comment);
		result.addObject("backURI",
				"availableItem/customer/edit.do?availableItemId="
						+ availableItemId);
		return result;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Comment comment, BindingResult binding) {
		ModelAndView result;

		if (binding.hasErrors()) {
			result = createEditModelAndView(comment);
		} else {
			try {
				int idAvailableItem;

				idAvailableItem = comment.getAvailableItem().getId();
				commentService.save(comment);
				result = new ModelAndView(
						"redirect:/availableItem/customer/edit.do?availableItemId="
								+ idAvailableItem);

			} catch (Throwable oops) {

				result = createEditModelAndView(comment, "comment.commit.error");
			}
		}

		return result;
	}

	// Listing methods
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Comment> comments;

		comments = commentService.findCommentsBySeller();

		result = new ModelAndView("comment/list");
		result.addObject("requestURI", "comment/customer/list.do");
		result.addObject("comments", comments);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/listOfOneItem", method = RequestMethod.GET)
	public ModelAndView listOfOneItem(@RequestParam int availableItemId) {
		ModelAndView result;
		Collection<Comment> comments;

		comments = commentService.findCommentsByItemId(availableItemId);

		result = new ModelAndView("comment/list");
		result.addObject("requestURI", "comment/customer/listOfOneItem.do");
		result.addObject("comments", comments);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/myRating", method = RequestMethod.GET)
	public ModelAndView myRating() {
		ModelAndView result;
		Double myRating;

		myRating = commentService.findAverageRatingBySeller();

		result = new ModelAndView("comment/myRating");
		result.addObject("requestURI", "comment/customer/myRating.do");
		result.addObject("myRating", myRating);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(Comment comment) {
		ModelAndView result;

		result = createEditModelAndView(comment, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Comment comment,
			String message) {
		ModelAndView result;

		result = new ModelAndView("comment/create");

		result.addObject("comment", comment);
		result.addObject("message", message);

		return result;
	}
}
